package org.example.blog_manage.Controller;


import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.example.blog_manage.model.Result;
import org.example.blog_manage.model.UserInfo;
import org.example.blog_manage.service.UserInfoService;
import org.example.blog_manage.utils.JwtUtils;
import org.example.blog_manage.utils.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping("/user")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("/login")
    public Result login(UserInfo userInfo){
//        参数判断
        if(userInfo==null|| !StringUtils.hasLength(userInfo.getUserName()) || !StringUtils.hasLength(userInfo.getPassword())){
            return Result.Fail("请合法输入");
        }
        UserInfo userInfo1 = userInfoService.selectByUserName(userInfo);
        log.info("接收到的密码为：{}",userInfo1);


        if(userInfo1.getPassword()==null|| !SecurityUtil.verify(userInfo.getPassword(),userInfo1.getPassword())){
            return Result.Fail("账号或密码错误");
        }

//        账号密码正确，生成token并返回
        Map<String,Object> claims = new HashMap<>();
        claims.put("id",userInfo1.getId());
        claims.put("userName",userInfo.getUserName());
        String token = JwtUtils.genToken(claims);
        log.info("设置客户端token，{}",token);
        return Result.success(token);
    }

    /*
    * 根据token获取当前用户信息
    * */
    @RequestMapping("/getUserInfo")
    public UserInfo getUserInfoById(HttpServletRequest request) {
        String token = request.getHeader("user_token_key");
        Integer infoID = null;
        if (StringUtils.hasLength(token)) {
            //获取用户id
            infoID = JwtUtils.getUserInfoID(token);
        }
        UserInfo userInfo = userInfoService.selectById(infoID);
        log.info("从token中获取的用户信息，{}",userInfo);
        return userInfo;
    }

    /*
    * 根据博客ID，获取博客作者信息
    * */
    @RequestMapping("/getAuthorInfo")
    public Result getAuthorInfo(Integer blogId){
        if(blogId!=null&&blogId>0){
            UserInfo userInfo = userInfoService.selectAuthorByBlogId(blogId);
            return Result.success(userInfo);
        }
        return Result.Fail("数据异常");
    }

    /*
    * 注册用户
    * */
    @RequestMapping("/enroll")
    public Result enroll(UserInfo userInfo){
//        参数判断
        if(userInfo==null|| !StringUtils.hasLength(userInfo.getUserName()) || !StringUtils.hasLength(userInfo.getPassword())){
            return Result.Fail("请合法输入");
        }

        String enroll = userInfoService.enroll(userInfo);
        if("注册成功".equals(enroll)){
            return Result.success("注册成功");
        }else {
            return Result.Fail("注册失败");
        }
    }
}
